package com.leon.document_auto_generation_tool.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * @author XiaoZeguang
 * @creatTime 2023-04-23
 */
public class FileUtils {

    private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);

    public static String bufferedRead(String originFilePath) throws IOException {
        FileReader fr = null;
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        String line;
        try {
            fr = new FileReader(originFilePath);
            br = new BufferedReader(fr);

            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            logger.error("读取文件[{}]时出错", originFilePath, e);
            throw e;
        } finally {
            IoUtils.closeAll(fr, br);
        }
        return sb.toString();
    }

    public static void bufferedWrite(String destFilePath, String data) throws IOException {
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
            fw = new FileWriter(destFilePath);
            bw = new BufferedWriter(fw);

            bw.write(data);
        } catch (IOException e) {
            logger.error("数据写入文件[{}]时出错", destFilePath, e);
            throw e;
        } finally {
            IoUtils.closeAll(fw, bw);
        }
    }

}
